<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.1.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"liukairui.me","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.10.1","exturl":false,"sidebar":{"position":"left","display":"hide","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":true,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="尚硅谷Linux运维集群部分, 包括多个阶段, 集群化阶段，讲解LVS与keepalived结合构建企业级负载调度集群;缓存阶段，由生产环境案例组成;存储阶段，糅合讲解 NFS、MFS、ISCSI 三类企业级存储技术; 监控阶段，包括Cacti、Nagios、Zabbix; 装机阶段，包括PXE、Cobbler,主讲:尚硅谷沈超与王洋,视频来自B站:BV1Db411G7pf">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux集群教程笔记">
<meta property="og:url" content="http://liukairui.me/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/">
<meta property="og:site_name" content="LiuKairui Website">
<meta property="og:description" content="尚硅谷Linux运维集群部分, 包括多个阶段, 集群化阶段，讲解LVS与keepalived结合构建企业级负载调度集群;缓存阶段，由生产环境案例组成;存储阶段，糅合讲解 NFS、MFS、ISCSI 三类企业级存储技术; 监控阶段，包括Cacti、Nagios、Zabbix; 装机阶段，包括PXE、Cobbler,主讲:尚硅谷沈超与王洋,视频来自B站:BV1Db411G7pf">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-31T16:00:01.000Z">
<meta property="article:modified_time" content="2021-10-31T19:02:19.348Z">
<meta property="article:author" content="Liu Kairui">
<meta property="article:tag" content="运维">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://liukairui.me/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://liukairui.me/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/","path":"article/Linux集群教程笔记/","title":"Linux集群教程笔记"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Linux集群教程笔记 | LiuKairui Website</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?ea07bceb8f5fa721da2ebf01833faa32"></script>



<div class="nindexBK">
<div class="nindexBKC"></div>
<div class="nindex">
  <h1>Hey👋</h1>
  <p style="font-weight: 500; font-size: 2.2em">欢迎访问Liu Kairui的个人站.</p>
  <p>
      我是一个来自重庆的在校大学生, 偏爱于前端技术<!--与OS设计-->, 偶尔使用 JS / Go 进行轻量项目开发, 欢迎访问我的
      <a href="https://github.com/KairuiLiu" target="_blank"><i class="fab fa-github-alt"></i> GitHub</a> .
  </p>
  <p>
      这个网站主要用来存储学习 / 瞎搞的笔记, 同时提供了邮件、图床等服务. 对于一些自认为有趣的<a href="javascript:;" id="myProj">项目</a>, 我也会将项目介绍, 文档, 甚至是设计指南发布在这里.
  </p>
  <ul>
    <li><p><a href="/artrank/" target="_blank">ArtRank</a>: 基于Node.JS的PageRank文献管理推荐系统</p></li>
    <li><p><a href="/gooj/" target="_blank">GoOJ</a>: 基于Golang的高校在线评测系统</p></li>
    <li><p><a href="/projects/" target="_blank">更多 <i class="fas fa-angle-right"></i></a></p></li>

  </ul>
  <p>
      可以在这些平台找到我: 
      <a href="https://www.zhihu.com/people/liu-kai-rui-18" target="_blank">知乎</a>,
      <a href="https://space.bilibili.com/33238144" target="_blank">bilibili</a>
      和
      <a href="https://www.coolapk.com/u/805314?from=qr" target="_blank">酷安</a>.
  </p>

  <p>
      下滑即可进入博客 <i class="fas fa-arrow-down"></i>
  </p>
</div>
</div>  

 
<script>
  var OriginTitile = document.title;
  var titleTime;
  document.addEventListener("visibilitychange", function() {
    if (document.hidden) {
      document.title = "谢谢离开 | " + OriginTitile;
      clearTimeout(titleTime);
    } else {
      document.title = "欢迎归来 | " + OriginTitile;
      titleTime = setTimeout(function() {
        document.title = OriginTitile;
      }, 2000);
    }
  });
</script>


<script>
  function checkIndex(){
    let nindexItems = [[".nindexBK","display","block","none"],[".headband","display","none","block"],["div.toggle.sidebar-toggle","visibility","hidden","visible"]]; // ,["button.darkmode-toggle","visibility","hidden","visible"]];
    let isIndex = new RegExp("^(\/){0,1}(index){0,1}(\.html){0,1}(/)*$").test(location.pathname);
    let backCanvas = document.querySelector(".nindexBK");
    nindexItems.dispShift = function (stat){this.forEach((e) => {try{document.querySelector(e[0]).style[e[1]]=e[2+stat];}catch{}})}
    if(isIndex){
      nindexItems.dispShift(0);
      window.onscroll=function(){
        if(document.documentElement.scrollTop>backCanvas.offsetHeight*1.05){
          nindexItems.dispShift(1);window.onscroll=null;
        }
      }
      document.querySelector("#myProj").onclick=()=>{
        let projlist = document.querySelector(".nindex>ul");
        projlist.className.indexOf("ulactive")==-1?projlist.classList.add("ulactive"):projlist.classList.remove("ulactive")
      }
    }else nindexItems.dispShift(1);
  }
  checkIndex();
</script>

<!--<script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>-->

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="LiuKairui Website" type="application/atom+xml">
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">LiuKairui Website</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">要有信仰</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fas fa-hashtag fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-收藏夹"><a href="/favorites/" rel="section"><i class="fab fa-gratipay fa-fw"></i>收藏夹</a></li>
        <li class="menu-item menu-item-留言板"><a href="/messageBoard/" rel="section"><i class="fab fa-facebook-messenger fa-fw"></i>留言板</a></li>
        <li class="menu-item menu-item-项目"><a href="/projects/" rel="section"><i class="fa fa-satellite fa-fw"></i>项目</a></li>
        <li class="menu-item menu-item-图床"><a href="http://img.liukairui.me/" rel="noopener" target="_blank"><i class="fa fa-bed fa-fw"></i>图床</a></li>
        <li class="menu-item menu-item-云ide"><a href="http://vsc.liukairui.me/" rel="noopener" target="_blank"><i class="fas fa-code fa-fw"></i>云IDE</a></li>
        <li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-邮箱"><a href="http://mail.liukairui.me/mail" rel="noopener" target="_blank"><i class="fas fa-envelope-open-text fa-fw"></i>邮箱</a></li>
        <li class="menu-item menu-item-rss"><a href="/atom.xml" rel="section"><i class="fa fa-rss fa-fw"></i>RSS</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#linux%E9%9B%86%E7%BE%A4"><span class="nav-number">1.</span> <span class="nav-text">Linux集群</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9B%86%E7%BE%A4%E6%A6%82%E8%BF%B0"><span class="nav-number">1.1.</span> <span class="nav-text">集群概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9B%86%E7%BE%A4%E5%88%86%E7%B1%BB"><span class="nav-number">1.1.1.</span> <span class="nav-text">集群分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4"><span class="nav-number">1.1.2.</span> <span class="nav-text">负载均衡集群</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F"><span class="nav-number">1.1.2.1.</span> <span class="nav-text">负载均衡工作模式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E9%AA%8C%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE"><span class="nav-number">1.1.2.2.</span> <span class="nav-text">实验环境配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#lvs-dr%E6%A8%A1%E5%BC%8F%E6%9E%84%E5%BB%BA"><span class="nav-number">1.1.2.3.</span> <span class="nav-text">LVS-DR模式构建</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#lvs-nat%E6%A8%A1%E5%BC%8F%E6%9E%84%E5%BB%BA"><span class="nav-number">1.1.2.4.</span> <span class="nav-text">LVS-NAT模式构建</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95"><span class="nav-number">1.1.2.5.</span> <span class="nav-text">负载均衡调度算法</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4"><span class="nav-number">1.1.3.</span> <span class="nav-text">高可用集群</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#keepalived"><span class="nav-number">1.1.3.1.</span> <span class="nav-text">KeepAlived</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#lvs-nat%E8%84%9A%E6%9C%AC%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="nav-number">1.1.3.2.</span> <span class="nav-text">LVS-NAT+脚本高可用负载均衡</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#lvs-drkeepalived%E9%AB%98%E5%8F%AF%E7%94%A8%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="nav-number">1.1.3.3.</span> <span class="nav-text">LVS-DR+KeepAlived高可用负载均衡</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#nginxheartbeat%E5%AE%9E%E7%8E%B0%E9%AB%98%E5%8F%AF%E7%94%A8"><span class="nav-number">1.1.3.4.</span> <span class="nav-text">Nginx+HeartBeat实现高可用</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E7%BA%A7%E8%B4%9F%E8%BD%BD"><span class="nav-number">1.1.4.</span> <span class="nav-text">多级负载</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Liu Kairui"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Liu Kairui</p>
  <div class="site-description" itemprop="description">LiuKairui Persional Website</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">53</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/KairuiLiu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;KairuiLiu" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:tclkr@live.com" title="E-Mail → mailto:tclkr@live.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://qm.qq.com/cgi-bin/qm/qr?k=oaf3ToOlN17hr5sHV98bT8qxsNYGaw5L&noverify=0" title="QQ → https:&#x2F;&#x2F;qm.qq.com&#x2F;cgi-bin&#x2F;qm&#x2F;qr?k&#x3D;oaf3ToOlN17hr5sHV98bT8qxsNYGaw5L&amp;noverify&#x3D;0" rel="noopener" target="_blank"><i class="fab fa-qq fa-fw"></i>QQ</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>RSS</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://liukairui.blog.csdn.net/" title="CSDN → https:&#x2F;&#x2F;liukairui.blog.csdn.net" rel="noopener" target="_blank"><i class="fab fa-cuttlefish fa-fw"></i>CSDN</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://liukairui.cc/" title="国内站点 → http:&#x2F;&#x2F;liukairui.cc" rel="noopener" target="_blank"><i class="fa fa-globe fa-fw"></i>国内站点</a>
      </span>
  </div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://paste.ubuntu.com/" title="https:&#x2F;&#x2F;paste.ubuntu.com&#x2F;" rel="noopener" target="_blank">Ubuntu Pastebin</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://send.firefox.com/" title="https:&#x2F;&#x2F;send.firefox.com&#x2F;" rel="noopener" target="_blank">Firefox Send</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://jkwzs.cn/" title="https:&#x2F;&#x2F;jkwzs.cn&#x2F;" rel="noopener" target="_blank">南花醉笔丶の个人博客</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://ywrby.cn/" title="https:&#x2F;&#x2F;ywrby.cn&#x2F;" rel="noopener" target="_blank">Ywrby个人博客</a>
        </li>
    </ul>
  </div>

        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://liukairui.me/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Liu Kairui">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="LiuKairui Website">
      <meta itemprop="description" content="LiuKairui Persional Website">
    </span>
    
    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Linux集群教程笔记 | LiuKairui Website">
      <meta itemprop="description" content="尚硅谷Linux运维集群部分, 包括多个阶段, 集群化阶段，讲解LVS与keepalived结合构建企业级负载调度集群;缓存阶段，由生产环境案例组成;存储阶段，糅合讲解 NFS、MFS、ISCSI 三类企业级存储技术; 监控阶段，包括Cacti、Nagios、Zabbix; 装机阶段，包括PXE、Cobbler,主讲:尚硅谷沈超与王洋,视频来自B站:BV1Db411G7pf">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Linux集群教程笔记
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-11-01 00:00:01" itemprop="dateCreated datePublished" datetime="2021-11-01T00:00:01+08:00">2021-11-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E8%BF%90%E7%BB%B4/" itemprop="url" rel="index"><span itemprop="name">运维</span></a>
        </span>
    </span>

  
    <span id="/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" class="post-meta-item leancloud_visitors" data-flag-title="Linux集群教程笔记" title="阅读次数">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span class="leancloud-visitors-count"></span>
    </span>
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">Valine：</span>
  
    <a title="valine" href="/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

<script>
    CONFIG.hostname = location.hostname;
</script>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>18k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>16 分钟</span>
    </span>
</div>

            <div class="post-description">尚硅谷Linux运维集群部分, 包括多个阶段, 集群化阶段，讲解LVS与keepalived结合构建企业级负载调度集群;缓存阶段，由生产环境案例组成;存储阶段，糅合讲解 NFS、MFS、ISCSI 三类企业级存储技术; 监控阶段，包括Cacti、Nagios、Zabbix; 装机阶段，包括PXE、Cobbler,主讲:尚硅谷沈超与王洋,视频来自B站:BV1Db411G7pf</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="linux集群">Linux集群</h1>
<h2 id="集群概述">集群概述</h2>
<p>集群是一组<strong>协同工作</strong>的服务器，<strong>各有分工</strong>，但是对外表现为<strong>一个整体</strong></p>
<p>单机模式成本低，时间长，效率低下，稳定性低，有性能极限，无法突破</p>
<p>集群模式成本高，效率高(1+1&gt;2)，适合大型业务，单机器宕机业务不中断</p>
<p>普通的单台服务器一般可以承受10万PV(点击量)的并发访问</p>
<h3 id="集群分类">集群分类</h3>
<ul>
<li>负载均衡集群LBC：有人负责指挥分配任务，有人负责工作，典型的是Web服务器。负责管理的是负载均衡服务器，可以是一个服务器实体，也可以是一个LVS软件模拟</li>
<li>高可用(故障转移)集群HAC：主服务器挂了后，备用服务器会自动切换过来继续承担主服务器的服务。主备服务器会通过keepalive模块进行检测(心跳线)，主挂掉后备自动启动。当备份服务器启动后，如果有自动断电机，会自动切断主服务器电源，防止备份服务器误判，主备同时开启。</li>
<li>高性能科学计算集群HPC：整合网络上有空闲资源的计算机，进行科学计算</li>
</ul>
<h3 id="负载均衡集群">负载均衡集群</h3>
<h4 id="负载均衡工作模式">负载均衡工作模式</h4>
<p>用户访问负载均衡服务器，负载均衡服务器将请求转发到业务服务器</p>
<ul>
<li><p>常见的负载均衡服务器软件有：LVS(工作在7层模型的传输层),Nginx(工作在7层模型的应用层),Haproxy</p></li>
<li><p>常见的负载均衡服务器硬件叫：F5(工作在7层模型的数据链路层)</p></li>
</ul>
<p><strong>LVS在网络传输层工作原理</strong></p>
<p>LVS工作在7层模型的传输层，也就是在五层模型传输而后数据网络层之间</p>
<p>用户与LVS服务器都工作在公网上，Apache服务器工作在内网上</p>
<p>用户发送请求到LVS服务器，LVS收到socket,上面有双方IP，LVS收到请求后使用规则库，将<strong>目标IP</strong>从自己的公网IP改为某个服务器的内网IP进行链接</p>
<p>精度低，负载大，可用于B/S与C/S架构</p>
<p><strong>Nginx网络应用层工作原理</strong></p>
<p>用户发起请求到Nginx，Nginx根据用户请求类型，<strong>重新</strong>与服务器建立TCP链接发送请求</p>
<p>相当于LVS是收到一个包裹，直接根据规则修改目的地，继续发送，但是Nginx是收到包裹，拆开，然后重新打单，做一个全新的链接，这是因为Nginx是工作在应用层的，所以LVS的负载均衡能力可能达到Nginx的几百倍，但是得益于此，Nginx可以对不同请求智能判断类型，转发到不同服务器</p>
<p>精度高，负载小，只能应用于B/S服务</p>
<p><strong>LVS在系统中工作方式</strong></p>
<p>操作系统可以分为内核空间和用户空间，用户空间安装了用户需要的程序，内核空间只有内核，LVS需要两个部分的程序：IPVS和IPVSadmin，其中IPVS工作在内核空间实现了OSI模型的部分功能，IPVSadmin工作在用户空间，实现了对IPVS的修改，LVS目前在用的有三种模式：NAT,TUN,DR模式，第四种模式目前正在开发</p>
<ul>
<li><p>NAT模式：</p>
<p>负载均衡服务器和用户在公网，服务器在内网</p>
<p>用户访问网站时访问到了负载均衡服务器的外网卡，LVS分配服务器，使用DNAT目标地址端口转换，通过内网网卡链接到服务器</p>
<p>服务器发送对象的时候发<strong>送到负载均衡服务器(否则用户看到源地址不对不收)</strong>，LVS使用SNAT源地址端口转换，修改socket的源地址为自己的外网IP,转发回去</p>
<p>特点是</p>
<ul>
<li>来回都要负载均衡服务器</li>
<li>负载均衡服务器一定程度扮演了网关的作用</li>
<li>支持端口映射</li>
</ul></li>
<li><p>TUN模式：</p>
<p>服务器，负载均衡服务器，用户都在外网，但是负载均衡服务器和工作服务器不在一个广播域</p>
<p>用户发送请求到负载均衡服务器，负载均衡服务器不修改目标IP，而是在数据包外面重封装了一个数据包，源IP是用户的，目标IP是真实服务器的</p>
<p>真实服务器去掉包装，处理数据，使用内层的目标IP(负载均衡服务器IP)作为源IP，使用内层的源IP(用户IP)作为目标IP，发送回应对象</p>
<p>特定是：</p>
<ul>
<li>两次借用公网IP，速度慢</li>
<li>压力大</li>
</ul></li>
<li><p>DR模式[负载能力最高]：</p>
<p>服务器，负载均衡服务器，用户都在外网，但是负载均衡服务器和工作服务器<strong>在一个广播域</strong>，也就是负载均衡服务器和工作服务器是同级别的</p>
<p>用户发起请求到负载均衡服务器，负载服务器转发到真实服务器，这次转发与NAT不同的是这次转发没有修改请求的目标IP,而是修改了请求的目标mac,由于负载均衡服务器和工作服务器是字啊一个广播域的，交换机可以通过MAC对请求进行转发，<strong>正常情况下，服务器收到了这个包，发现mac是自己的，但是IP不是自己的，不会收，但是我们在服务器上多设置了一个IP，IP地址与负载均衡服务器是一样的，于是服务器就会收下，但是这个IP是不能暴露的，只能用于通过MAC收到数据包验证用，否则就IP冲突了，于是我们需要把IP隐藏起来</strong>，</p>
<p>返回文件的时候，服务器<strong>不经过负</strong>载均衡服务器，直接用隐藏的IP发送数据包，客户机看到双方IP与端口都是对的，于是接受。</p>
<p>特点是：</p>
<ul>
<li><p>由于负载均衡服务器转发使用的是MAC,所以必须在一个广播域</p></li>
<li><p>真实服务器回应无需负载均衡服务器(高并发原因)</p></li>
<li><p>不支持端口映射(根本就没有触碰到传输层)</p></li>
</ul></li>
<li><p>TUN和DR的区别就是DR的机器都在一个广播域，可以通过修改MAC的方式实现数据包的转发，但是TUN不再一个广播域，就必须改IP，最后两者返回数据的方式都是将IP改为负载服务器IP返回的</p></li>
</ul>
<h4 id="实验环境配置">实验环境配置</h4>
<ul>
<li><p>物理机安装<strong>Chrome</strong>，由于浏览器缓存，可能无法明显感知服务器的切换，安装后使用无痕模式可以解决这个问题</p></li>
<li><p>6台CentOS7 BaseServer 服务器</p></li>
<li><p>1台CentOS7 GUI 桌面服务器</p></li>
<li><p>关闭防火墙 SELinux</p></li>
<li><p>网络选择仅主机(Host-Only)</p>
<p>配置网卡地址10.10.10.01/24</p>
<p>关闭DHCP服务器</p>
<p>虚拟机IP从10.10.10.11开始命名</p></li>
<li><p>虚拟机配置双网卡 全部是HostOnly==?==</p></li>
<li><p>1G内存</p></li>
<li><p>配置6台机器为静态IP</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">vim</span> /etc/sysconfig/network-scripts/ifcfg-enp0s3
    <span class="token comment"># 修改</span>
    <span class="token assign-left variable">BOOTPROTO</span><span class="token operator">=</span><span class="token string">"static"</span>
    <span class="token assign-left variable">IPADDR</span><span class="token operator">=</span><span class="token number">10.10</span>.10.11
    <span class="token assign-left variable">NETMASK</span><span class="token operator">=</span><span class="token number">255.255</span>.255.0
systemctl restart network.service </code></pre>
<p>使用物理机ssh尝试连接</p></li>
<li><p>配置本地yum源</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">mkdir</span> /mnt/cdrom
<span class="token function">mount</span> -t iso9660 /dev/cdrom /mnt/cdrom/
<span class="token builtin class-name">cd</span> /etc/yum.repos.d/
<span class="token function">mkdir</span> ./CentBak
<span class="token function">mv</span> *.repo CentBak/
<span class="token function">cp</span> ./CentBak/CentOS-Media.repo ./
<span class="token function">vim</span> CentOS-Media.repo
    <span class="token assign-left variable">baseurl</span><span class="token operator">=</span>file:///media/CentOS/
            file:///media/cdrom/
            file:///media/cdrecorder/
    <span class="token assign-left variable">gpgcheck</span><span class="token operator">=</span><span class="token number">1</span>
    <span class="token assign-left variable">enabled</span><span class="token operator">=</span><span class="token number">0</span>
    <span class="token comment"># 改为</span>
    <span class="token assign-left variable">baseurl</span><span class="token operator">=</span>file:///mnt/cdrom	<span class="token comment"># 改路径</span>
    <span class="token assign-left variable">gpgcheck</span><span class="token operator">=</span><span class="token number">0</span>					<span class="token comment"># 不检查gpg</span>
    <span class="token assign-left variable">enabled</span><span class="token operator">=</span><span class="token number">1</span>					<span class="token comment"># 启动</span>
yum clean all <span class="token operator">&amp;&amp;</span> yum makecache
yum <span class="token function">install</span> gcc gcc-c++ lrzsz	<span class="token comment"># 安装编译器 文件上传工具(如果是xshell)</span></code></pre></li>
</ul>
<h4 id="lvs-dr模式构建">LVS-DR模式构建</h4>
<p>需要三台虚拟机：</p>
<ul>
<li>机器C：客户机 物理机 192.168.177.162</li>
<li>机器D：负载均衡服务器 虚拟机1 网卡1(enpxxx)：10.10.10.11
网卡1(enpxxx)浮动：10.10.10.100</li>
<li>机器RS1：服务器1 虚拟机2 网卡1(enpxxx)：10.10.10.12
网卡0(lo)浮动：10.10.10.100</li>
<li>机器RS2：服务器2 虚拟机3 网卡1(enpxxx)10.10.10.13
网卡0(lo)浮动：10.10.10.100</li>
</ul>
<p><strong>原理</strong></p>
<ul>
<li><p>物理机C访问负载均衡服务器D网卡2 10.10.10.100</p></li>
<li><p>负载均衡服务器D进行负载均衡计算，修改DMAC，用网卡1 10.10.10.11
发到广播域</p></li>
<li><p>其他服务器修改lo卡 为10.10.10.100，接受数据报文</p></li>
</ul>
<p><strong>ARP 通讯行为</strong></p>
<p>ARP响应级别(arp-ignore)：数据报文到达网口后是否响应</p>
<ul>
<li><p>0级别(默认)：只要收到了本机配置的IP就响应</p></li>
<li><p>1级别：只有发送到了接受网卡的IP上才响应</p>
<p>例如负载均衡服务器D有10.10.10.11 和 10.10.10.100
两张卡，10.10.10.11卡收到了一个发给10.10.10.100卡的报文，如果是0级别，发现本机有10.10.10.100
于是会手下，如果是1级别，发现不是本卡IP10.10.10.11就不接受</p></li>
</ul>
<p>ARP通告级别：</p>
<ul>
<li>0级别：将本机上的<strong>所有</strong>的网络接口地址都进行通告(例如网卡上电后会向网络中中广播自己的地址)</li>
<li>1级别：尽可能避免0的情况，倾向于2,但是不保证，尽量仅向该网卡回应与该网段匹配的ARP报文</li>
<li>2级别：只向网络中通告自己的IP，MAC</li>
</ul>
<p>这样，我们将配置修改为1,2，这样实际服务器的网卡2lo
不会主动接受发给10.10.10.12的消息，也不往外说</p>
<p><strong>负载均衡服务器操作</strong></p>
<ul>
<li><p>关闭所有虚拟机的<code>NetworkManager</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@C7B1 ~<span class="token punctuation">]</span><span class="token comment"># systemctl stop NetworkManager</span>
<span class="token punctuation">[</span>root@C7B1 ~<span class="token punctuation">]</span><span class="token comment"># systemctl disable NetworkManager</span></code></pre>
<p>这个服务主要是负责Linux的无线网络服务和危险网络策略的关闭，我们需要关闭这个服务，注意：服务名区分大小写</p></li>
<li><p>开启网卡的子接口：也就是一块网卡两个IP地址</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@C7B1 network-scripts<span class="token punctuation">]</span><span class="token comment"># cp ifcfg-enp0s3 ifcfg-enp0s3:0</span>
<span class="token punctuation">[</span>root@C7B1 network-scripts<span class="token punctuation">]</span><span class="token comment"># vim !$						# !$ 上一条命令最后一个参数</span></code></pre>
<p>修改复制过去的配置文件</p>
<pre class="language-diff" data-language="diff"><code class="language-diff"><span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"> IPADDR=10.10.10.11
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line"> IPADDR=10.10.10.100
</span></span><span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"> DEVICE="enp0s3"
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line"> DEVICE="enp0s3:0"
</span></span><span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"> NAME="enp0s3"
</span><span class="token prefix deleted">-</span><span class="token line"> UUID="9b24b2da-f8e2-45c9-81dd-e3b364066858"</span></span></code></pre>
<p>重启网络服务</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@C7B1 network-scripts<span class="token punctuation">]</span><span class="token comment"># systemctl restart network</span></code></pre>
<p>这里复制的接口IP相当于是浮动IP,可以认为原来的IP是绝对IP,浮动IP通常是一个公开的、可以路由到的IP地址，并且不会自动分配给实体设备。项目管理者临时分配动态IP到一个或者多个实体设备。这个实体设备有自动分配的静态IP用于内部网间设备的通讯。这个内部网使用私有地址，这些私有地址不能被路由到。通过浮动IP内网实体的服务才能被外网识别和访问。在一个配置好浮点IP的切换场景是，IP地址飘到网络中的另一台设备。新设备无延迟的接替当掉的设备，并对外提供服务。</p></li>
<li><p>修改内核文件<code>/etc/sysctl.conf</code>设置ARP关闭网卡的广播，这是一个优化功能，不强制要求，追加</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">net.ipv4.conf.all.send_redirects <span class="token operator">=</span> <span class="token number">0</span> 
net.ipv4.conf.default.send_redirects <span class="token operator">=</span> <span class="token number">0</span> 
net.ipv4.conf.enp0s3.send_redirects <span class="token operator">=</span> <span class="token number">0</span> </code></pre>
<p><code>sysctl -p</code>刷新内核</p></li>
<li><p>安装ipvsadm</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">yum <span class="token function">install</span> ipvsadm</code></pre></li>
<li><p>将ipvs加入到系统内核</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">modprobe ip_vs</code></pre></li>
<li><p>配置IPVS</p>
<p><code>ipvsadm</code>命令用来添加LVS规则</p>
<ul>
<li><code>-A</code>添加一个集群</li>
<li><code>-a</code>添加集群的节点</li>
<li><code>-t</code> tcp协议</li>
<li><code>-s</code>算法</li>
</ul>
<pre class="language-bash" data-language="bash"><code class="language-bash">ipvsadm -A -t <span class="token number">10.10</span>.10.100:80 -s rr			<span class="token comment"># 轮询</span>
ipvsadm -a -t <span class="token number">10.10</span>.10.100:80 -r <span class="token number">10.10</span>.10.12:80 -g
ipvsadm -a -t <span class="token number">10.10</span>.10.100:80 -r <span class="token number">10.10</span>.10.13:80 -g</code></pre>
<p>保存配置的规则</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">ipvsadm -S <span class="token operator">></span> /etc/sysconfig/ipvsadm</code></pre></li>
</ul>
<p><strong>真实服务器的操作</strong></p>
<ul>
<li><p>启动Apache用于测试，写一个网页用来标识</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@C7B2 ~<span class="token punctuation">]</span><span class="token comment"># systemctl start httpd</span>
<span class="token punctuation">[</span>root@C7B2 ~<span class="token punctuation">]</span><span class="token comment"># echo "S1" > /var/www/html/index.html</span></code></pre></li>
<li><p>设置浮动IP，对lo卡复制接口，因为LVS服务器在广播自己是
10.10.10.100，你不能在一个内网中广播地址中设置同IP了(当然可以通过后面修改启动文件解决)，于是使用了广播地址不同的lo卡</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">DEVICE</span><span class="token operator">=</span>lo:0
<span class="token assign-left variable">IPADDR</span><span class="token operator">=</span><span class="token number">10.10</span>.10.100
<span class="token assign-left variable">NETMASK</span><span class="token operator">=</span><span class="token number">255.255</span>.255.255			<span class="token comment"># 修改子网掩码防止和真实IP冲突</span>
<span class="token assign-left variable">NETWORK</span><span class="token operator">=</span><span class="token number">127.0</span>.0.0</code></pre></li>
<li><p>设置ARP,<code>/etc/sysctl.conf</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">net.ipv4.conf.all.arp_ignore <span class="token operator">=</span> <span class="token number">1</span>
net.ipv4.conf.all.arp_announce <span class="token operator">=</span> <span class="token number">2</span>
net.ipv4.conf.default.arp_ignore <span class="token operator">=</span> <span class="token number">1</span>
net.ipv4.conf.default.arp_announce <span class="token operator">=</span> <span class="token number">2</span>
net.ipv4.conf.lo.arp_ignore <span class="token operator">=</span> <span class="token number">1</span>
net.ipv4.conf.lo.arp_announce <span class="token operator">=</span> <span class="token number">2</span></code></pre>
<p>重启网络服务</p></li>
<li><p>当负载均衡服务器转发请求到lo:0的时候需要有网关进行转交</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@C7B2 network-scripts<span class="token punctuation">]</span><span class="token comment"># route add -host 10.10.10.100 dev lo:0</span>
<span class="token punctuation">[</span>root@C7B2 network-scripts<span class="token punctuation">]</span><span class="token comment"># echo "route add -host 10.10.10.100 dev lo:0" >> /etc/rc.local 		# 添加到自启动</span></code></pre></li>
</ul>
<p><strong>在物理机上访问10.10.10.100</strong></p>
<p>使用<code>ipvsadm -Ln --stats</code>查看状态</p>
<p><strong>关闭DR模式</strong>：</p>
<ul>
<li>LVS服务器：移除<code>/etc/sysctl.conf</code></li>
<li>LVS服务器：关闭LVS<code>ipvsadm -D -t 10.10.10.100:80</code></li>
<li>LVS服务器：删除LVS服务器配置文件<code>/etc/sysconfig/ipvsadm</code></li>
<li>LVS服务器：删除浮动IP配置</li>
<li>真实服务器：删除浮动IP配置文件</li>
<li>真实服务器：删除ARP配置<code>/etc/sysctl.conf</code></li>
<li>真实服务器：关闭网关自启<code>/etc/rc.local</code></li>
</ul>
<h4 id="lvs-nat模式构建">LVS-NAT模式构建</h4>
<p>需要三台虚拟机：</p>
<ul>
<li>机器C：客户机 物理机 192.168.177.162</li>
<li>机器D：负载均衡服务器 虚拟机1 内网卡1(enpxxx)：10.10.10.11
公网卡2(enpxxx)：20.20.20.11</li>
<li>机器RS1：服务器1 虚拟机2 内网卡1(enpxxx)：10.10.10.12</li>
<li>机器RS2：服务器2 虚拟机3 内网卡1(enpxxx)10.10.10.13</li>
</ul>
<p><strong>负载均衡服务器</strong></p>
<ul>
<li><p>关闭NetworkMangaer</p></li>
<li><p>安装ipvsadm</p></li>
<li><p>编辑文件 开启路由转发<code>/etc/sysctl.conf</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">et.ipv4.ip_forward <span class="token operator">=</span> <span class="token number">1</span></code></pre>
<p><code>sysctl -p</code>启用</p></li>
<li><p>开启防火墙SNAT转发(服务器到客户端)</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">systemctl start firewalld
firewall-cmd --zone<span class="token operator">=</span>trusted --change-interface<span class="token operator">=</span>enp0s3
firewall-cmd --zone<span class="token operator">=</span>trusted --change-interface<span class="token operator">=</span>enp0s8
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s <span class="token number">10.10</span>.10.0/24 -j SNAT --to-source <span class="token number">20.20</span>.20.11
<span class="token comment"># 将所有10.10.10.0网段的数据(真实服务器的)使用外网网卡转发</span></code></pre></li>
<li><p>配置ipvs(客户端到服务器)</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">ipvsadm -D -t <span class="token number">10.10</span>.10.100:80				<span class="token comment"># 清除DR模式的规则</span>
ipvsadm -A -t <span class="token number">20.20</span>.20.11:80 -s rr
ipvsadm -a -t <span class="token number">20.20</span>.20.11:80 -r <span class="token number">10.10</span>.10.12:80 -m
ipvsadm -a -t <span class="token number">20.20</span>.20.11:80 -r <span class="token number">10.10</span>.10.13:80 -m
ipvsadm -S <span class="token operator">></span> /etc/sysconfig/ipvsadm
ipvsadm -Ln</code></pre></li>
<li><p>配置网关,就是服务器什么要发的都先发给负载均衡，追加网卡配置文件(服务器到客户端)</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">GATEWAY</span><span class="token operator">=</span><span class="token number">10.10</span>.10.11</code></pre></li>
</ul>
<p><strong>关闭NAT模式</strong>：</p>
<ul>
<li><p>LVS服务器：关闭路由转发路由转发<code>/etc/sysctl.conf</code></p></li>
<li><p>LVS服务器：关闭SNAT</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">firewall-cmd --permanent --direct --remove-passthrough ipv4 -t nat -A POSTROUTING -s <span class="token number">10.10</span>.10.0/24 -j SNAT --to-source <span class="token number">20.20</span>.20.11</code></pre></li>
<li><p>客户服务器：修改网卡网关配置</p></li>
</ul>
<h4 id="负载均衡调度算法">负载均衡调度算法</h4>
<p>有通用算法和LVS持久链接算法，后者是仅对LVS有效</p>
<p><strong>通用固定算法</strong>：固定算法指的是不考虑服务器的实际状况，直接使用算法产生调度策略</p>
<ul>
<li>RR轮询算法：将请求按顺序循环分配给后端服务器</li>
<li>WRR加权轮询算法：按照一定的权重进行轮询，例如给配置好的服务器大权重，访问次数多一点</li>
<li>SH源地址散列算法：将来源IP做hash，选定服务器，一个IP每次都链接到了一个服务器，优点就是对cookie优化良好(对不使用分布式存储服务器)</li>
<li>DH目标地址散列算法：方便用户高概率命中特定类型的缓存</li>
</ul>
<p><strong>通用动态算法</strong>：</p>
<ul>
<li><p>LC最少连接算法：将新连接分配给连接数最少的服务器，计算方式是**活动连接数*256+非活动连接**</p>
<p>活动连接指的是正在传输数据的连接</p>
<p>非活动连接指的是刚刚握手或者握手结束的连接</p></li>
<li><p>WLC加权最少连接：将新连接分配给加权后连接数最少的服务器，也就是**(活动连接数*256+非活动连接)/权重**</p></li>
<li><p>SED最短期望延时：WLC存在一个问题，当计算结果相同的时候会将连接按照服务器列表顺序分配，可能大权重服务器在后面，我们想将请求分配到大权服务器，但是按照WLC的原理可能会分配给小权服务器，于是有了SED**(活动数+1)*256/权重**，他保证了第二关键字为权重</p></li>
<li><p>NQ永不排队算法：特殊的SED，如果有连接数为0的空闲服务器就直接分配</p></li>
<li><p>LBLC算法：特殊的DH算法，当负载均衡服务器发现某个特定的缓存服务器压力过大的时候会自动再开一个缓存服务器轮询提供缓存服务</p></li>
<li><p>LBRCR算法：支持多缓存服务器数据交互</p></li>
</ul>
<p><strong>LVS持久化连接</strong></p>
<ul>
<li>SSL等协议需要建立消耗资源的握手，但是非SH算法会造成重复多次消耗资源的握手</li>
<li>LVS引入了持久化连接实现了类似SH和RR的融合使用</li>
<li>负载均衡服务器记录了客户端与服务器的连接对应关系，当持久化倒计时结束后会进行轮询</li>
</ul>
<p>分类：</p>
<ul>
<li><p>PCC持久客户端连接：客户端访问LVS集群的时候，同一个客户端IP会重定向到一个服务器</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">ipvsadm -A -t <span class="token number">20.20</span>.20.11:80 -s wlc -p <span class="token number">120</span></code></pre></li>
<li><p>PPC持久端口连接：客户端访问LVS集群的时候，同一个客户端的同一个端口会重定向到一个服务器</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">ipvsadm -A -t <span class="token number">20.20</span>.20.11:80 -s rr -p <span class="token number">120</span></code></pre></li>
<li><p>PFMC持久防火墙标记连接：根据客户端的源地址，端口，数据端属性等对客户进行标记，根据标记分配到不同客户</p></li>
<li><p>使用<code>ipvsadm -Ln --persistent-conn</code>看到处于可持久化连接</p></li>
</ul>
<h3 id="高可用集群">高可用集群</h3>
<p>通过心跳检测将不可用集群踢出ipvs</p>
<h4 id="keepalived">KeepAlived</h4>
<p>KeepAlived是专门为LVS/HA设置的健康检查工具，支持鼓掌自动切换与节点状态检查</p>
<p>KeepAlived使用VRRP虚拟路由冗余协议，这个协议是一个浮动IP的竞争协议，一个节点挂掉之后，其他的节点会竞争浮动IP，选出的服务器会继续使用这个浮动IP</p>
<h4 id="lvs-nat脚本高可用负载均衡">LVS-NAT+脚本高可用负载均衡</h4>
<p>LVS默认是不支持高可用KeepAlive的，可以用户脚本实现</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">mkdir</span> /usr/local/script
<span class="token builtin class-name">cd</span> /usr/local/script
<span class="token function">vim</span> kav.sh
<span class="token function">chmod</span> <span class="token number">755</span> kav.sh </code></pre>
<p>写入脚本</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>
<span class="token comment">#检测lvs集群节点是否正常，不正常则踢出集群</span>

<span class="token assign-left variable">VIP</span><span class="token operator">=</span><span class="token number">20.20</span>.20.11	<span class="token comment">#集群虚拟IP</span>
<span class="token assign-left variable">CPORT</span><span class="token operator">=</span><span class="token number">80</span>	<span class="token comment">#定义集群端口</span>
<span class="token assign-left variable">FAIL_BACK</span><span class="token operator">=</span><span class="token number">127.0</span>.0.1	<span class="token comment">#本机回环地址</span>
<span class="token assign-left variable">RS</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"10.10.10.12"</span> <span class="token string">"10.10.10.13"</span><span class="token punctuation">)</span>	<span class="token comment">#编写集群地址</span>
<span class="token builtin class-name">declare</span> -a RSSTATUS  <span class="token comment">#变量RSSTATUS定义为数组态</span>
<span class="token assign-left variable">RW</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">"2"</span> <span class="token string">"1"</span><span class="token punctuation">)</span>
<span class="token assign-left variable">RPORT</span><span class="token operator">=</span><span class="token number">80</span>	<span class="token comment">#定义集群端口</span>
<span class="token assign-left variable">TYPE</span><span class="token operator">=</span>g	  <span class="token comment">#制定LVS工作模式：g=DR m=NAT</span>
<span class="token assign-left variable">CHKLOOP</span><span class="token operator">=</span><span class="token number">3</span>
<span class="token assign-left variable">LOG</span><span class="token operator">=</span>/var/log/ipvsmonitor.log

<span class="token comment">#添加RS，添加成功返回0，否则返回1</span>
<span class="token function-name function">addrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  ipvsadm -a -t <span class="token variable">$VIP</span><span class="token builtin class-name">:</span><span class="token variable">$CPORT</span> -r <span class="token variable">$1</span><span class="token builtin class-name">:</span><span class="token variable">$RPORT</span> -<span class="token variable">$TYPE</span> -w <span class="token variable">$2</span>
  <span class="token punctuation">[</span> <span class="token variable">$?</span> -eq <span class="token number">0</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">return</span> <span class="token number">0</span> <span class="token operator">||</span> <span class="token builtin class-name">return</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">#删除RS，删除成功返回0，否则返回1</span>
<span class="token function-name function">delrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  ipvsadm -d -t <span class="token variable">$VIP</span><span class="token builtin class-name">:</span><span class="token variable">$CPORT</span> -r <span class="token variable">$1</span><span class="token builtin class-name">:</span><span class="token variable">$RPORT</span>
  <span class="token punctuation">[</span> <span class="token variable">$?</span> -eq <span class="token number">0</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">return</span> <span class="token number">0</span> <span class="token operator">||</span> <span class="token builtin class-name">return</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">#检测RS服务是否在线，注意一下这里面指的RS的服务，如果连续三次都监测不通，则返回1，否则返回0</span>
<span class="token function-name function">checkrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token builtin class-name">local</span> <span class="token assign-left variable">I</span><span class="token operator">=</span><span class="token number">1</span>
  <span class="token keyword">while</span> <span class="token punctuation">[</span> <span class="token variable">$I</span> -le <span class="token variable">$CHKLOOP</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
	<span class="token keyword">if</span> <span class="token function">curl</span> --connect-timeout <span class="token number">1</span> http://<span class="token variable">$1</span> <span class="token operator">&amp;></span> /dev/null<span class="token punctuation">;</span> <span class="token keyword">then</span>
	  <span class="token builtin class-name">return</span> <span class="token number">0</span>
	<span class="token keyword">fi</span>
	<span class="token builtin class-name">let</span> I++
  <span class="token keyword">done</span>
  <span class="token builtin class-name">return</span> <span class="token number">1</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">#初始化RS在线状态，如果在线，设置节点初始化状态为1，否则为0</span>
<span class="token function-name function">initstatus</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
  <span class="token builtin class-name">local</span> I
  <span class="token builtin class-name">local</span> <span class="token assign-left variable">COUNT</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token for-or-select variable">I</span> <span class="token keyword">in</span> <span class="token variable">$&#123;RS<span class="token punctuation">[</span>*<span class="token punctuation">]</span>&#125;</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
	<span class="token keyword">if</span> ipvsadm -L -n <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">"<span class="token variable">$I</span>:<span class="token variable">$RPORT</span>"</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">></span> /dev/null <span class="token punctuation">;</span> <span class="token keyword">then</span>
	  RSSTATUS<span class="token punctuation">[</span><span class="token variable">$COUNT</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">1</span>
	<span class="token keyword">else</span>
	  RSSTATUS<span class="token punctuation">[</span><span class="token variable">$COUNT</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span>
	<span class="token keyword">fi</span>
  <span class="token builtin class-name">let</span> COUNT++
  <span class="token keyword">done</span>
<span class="token punctuation">&#125;</span>

<span class="token comment">#进行初始化</span>
initstatus

<span class="token comment">#监测rs是否加入到ipvs</span>
	  <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$&#123;RSSTATUS<span class="token punctuation">[</span>$COUNT<span class="token punctuation">]</span>&#125;</span> -eq <span class="token number">1</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
		 delrs <span class="token variable">$I</span>
		 <span class="token punctuation">[</span> <span class="token variable">$?</span> -eq <span class="token number">0</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> RSSTATUS<span class="token punctuation">[</span><span class="token variable">$COUNT</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable"><span class="token variable">`</span><span class="token function">date</span> +<span class="token string">'%F %H:%M:%S'</span><span class="token variable">`</span></span>, <span class="token variable">$I</span> is gone."</span> <span class="token operator">>></span> <span class="token variable">$LOG</span>
	  <span class="token keyword">fi</span>
	<span class="token keyword">fi</span>
	<span class="token builtin class-name">let</span> COUNT++
  <span class="token keyword">done</span>
  <span class="token function">sleep</span> <span class="token number">5</span>
<span class="token keyword">done</span></code></pre>
<p>运行即可，关闭20.20.20.12的httpd后，查看<code>ipvsadm -Ln</code>服务器少了一个</p>
<p><strong>LVS高可用配置</strong>：在这样的高可用中，如果LVS挂掉，整个服务就挂掉了，所以，还需要配置多个LVS以实现高可用</p>
<h4
id="lvs-drkeepalived高可用负载均衡">LVS-DR+KeepAlived高可用负载均衡</h4>
<p>需要四台虚拟机：</p>
<ul>
<li>机器C：客户机 物理机 192.168.177.162</li>
<li>机器LVS-M：主高可用负载均衡服务器1 虚拟机1
网卡1(enpxxx)：10.10.10.11 网卡1(enpxxx)浮动：10.10.10.100</li>
<li>机器LVS-S：备份高可用负载均衡服务器2 虚拟机<strong>4</strong>
网卡1(enpxxx)：10.10.10.14 网卡1(enpxxx)浮动：10.10.10.100</li>
<li>机器RS1：服务器1 虚拟机2 网卡1(enpxxx)：10.10.10.12
网卡0(lo)浮动：10.10.10.100</li>
<li>机器RS2：服务器2 虚拟机3 网卡1(enpxxx)10.10.10.13
网卡0(lo)浮动：10.10.10.100</li>
</ul>
<p>首先是LVS-M作为主负载均衡服务器运行，心跳检测RS1与RS2，宕机就踢出去，同时LVS-S作为从负载均衡服务器心跳检测LVS-M，如果主负载均衡服务器宕机就立马接替</p>
<p><strong>LVS-M,RS1,RS2配置</strong></p>
<ul>
<li>按照普通LVS-DR构建集群，使用虚拟机1,2,3</li>
</ul>
<p><strong>主负载均衡心跳检测服务器LVS-M</strong></p>
<ul>
<li><p>安装keeplived(光盘源有)</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">yum <span class="token function">install</span> -y keepalived</code></pre></li>
<li><p>启动并设置自启动</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">systemctl start keepalived
systemctl <span class="token builtin class-name">enable</span> keepalived</code></pre></li>
<li><p>配置文件：<code>/etc/keepalived/keepalived.conf</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token operator">!</span> Configuration File <span class="token keyword">for</span> keepalived

global_defs <span class="token punctuation">&#123;</span>
   notification_email 
     <span class="token punctuation">..</span>.
   smtp_connect_timeout <span class="token number">30</span>		<span class="token comment"># 挂掉后邮件通知配置 我们不使用</span>
   router_id LVS_DEVEL			<span class="token comment"># 当前机器在keepalive的别名</span>
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval <span class="token number">0</span>
   vrrp_gna_interval <span class="token number">0</span>
<span class="token punctuation">&#125;</span>

vrrp_instance VI_1 <span class="token punctuation">&#123;</span>
    state MASTER				<span class="token comment"># 这是什么服务器</span>
    interface eth0				<span class="token comment"># 用哪个网卡检测</span>
    virtual_router_id <span class="token number">51</span>		<span class="token comment"># 当前高可用ID</span>
    priority <span class="token number">80</span>		 		    <span class="token comment"># 权重设置 建议主比一级从高50 </span>
    advert_int <span class="token number">1</span>				<span class="token comment"># 心跳时间</span>
    authentication <span class="token punctuation">&#123;</span>			<span class="token comment"># 身份认证模式</span>
        auth_type PASS			<span class="token comment"># 密码认证</span>
        auth_pass <span class="token number">1111</span>			<span class="token comment"># 密码</span>
    <span class="token punctuation">&#125;</span>
    virtual_ipaddress <span class="token punctuation">&#123;</span>
        <span class="token number">192.168</span>.200.16			<span class="token comment"># 集群LVS的IP地址</span>
        <span class="token number">192.168</span>.200.17
        <span class="token number">192.168</span>.200.18
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

virtual_server <span class="token number">192.168</span>.200.100 <span class="token number">443</span> <span class="token punctuation">&#123;</span>	<span class="token comment"># LVS服务器IP 端口</span>
    delay_loop <span class="token number">6</span>				<span class="token comment"># 检测间隔</span>
    lb_algo rr					<span class="token comment"># 轮询设置</span>
    lb_kind NAT					<span class="token comment"># LVS模式</span>
    persistence_timeout <span class="token number">50</span>		<span class="token comment"># 检测时间</span>
    protocol TCP

    real_server <span class="token number">192.168</span>.201.100 <span class="token number">443</span> <span class="token punctuation">&#123;</span>	<span class="token comment"># 真实服务器信息</span>
        weight <span class="token number">1</span>						<span class="token comment"># 权重</span>
        SSL_GET <span class="token punctuation">&#123;</span>
            url <span class="token punctuation">&#123;</span>
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            <span class="token punctuation">&#125;</span>
            url <span class="token punctuation">&#123;</span>
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            <span class="token punctuation">&#125;</span>
            connect_timeout <span class="token number">3</span>
            nb_get_retry <span class="token number">3</span>
            delay_before_retry <span class="token number">3</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span></code></pre>
<p>进行配置</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">global_defs <span class="token punctuation">&#123;</span>
   router_id KPA01
<span class="token punctuation">&#125;</span>

vrrp_instance VI_1 <span class="token punctuation">&#123;</span>
    state MASTER
    interface enp0s3
    virtual_router_id <span class="token number">66</span>
    priority <span class="token number">80</span>
    advert_int <span class="token number">1</span>
    authentication <span class="token punctuation">&#123;</span>
        auth_type PASS
        auth_pass <span class="token number">1111</span>
    <span class="token punctuation">&#125;</span>
    virtual_ipaddress <span class="token punctuation">&#123;</span>
		<span class="token number">10.10</span>.10.100
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

virtual_server <span class="token number">10.10</span>.10.100 <span class="token number">80</span> <span class="token punctuation">&#123;</span>
    delay_loop <span class="token number">2</span>					<span class="token comment"># 监测频率</span>
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server <span class="token number">10.10</span>.10.12 <span class="token number">80</span> <span class="token punctuation">&#123;</span>	<span class="token comment"># 管理的网站节点以及使用端口 </span>
        weight <span class="token number">1</span>
	TCP_CHECK <span class="token punctuation">&#123;</span>
	    connect_port <span class="token number">80</span> 			<span class="token comment"># 检查的目标端口 </span>
	    connect_timeout <span class="token number">3</span> 			<span class="token comment"># 连接超时（秒） </span>
	    nb_get_retry <span class="token number">3</span> 				<span class="token comment"># 重试次数 </span>
	    delay_before_retry <span class="token number">4</span>		<span class="token comment"># 重试间隔（秒）</span>
	<span class="token punctuation">&#125;</span> 
    <span class="token punctuation">&#125;</span>
    real_server <span class="token number">10.10</span>.10.13 <span class="token number">80</span> <span class="token punctuation">&#123;</span>
        weight <span class="token number">1</span>
        TCP_CHECK <span class="token punctuation">&#123;</span>                
            connect_port <span class="token number">80</span>                   
            connect_timeout <span class="token number">3</span>                 
           nb_get_retry <span class="token number">3</span>           
            delay_before_retry <span class="token number">4</span>
        <span class="token punctuation">&#125;</span> 
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span></code></pre></li>
</ul>
<p>查看状态</p>
<p><pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">cat</span> /var/log/messages </code></pre></p>
<p>看到有：
<code>Sending gratuitous ARP on enp0s3 for xxx.xxx.xxx.xxx</code></p>
<p>看到负载均衡允许正常，真实服务器关闭后，<code>ipvsadm -Ln</code>自动踢走</p>
<p><strong>备份负载均衡心跳检测服务器LVS-S</strong></p>
<p>我们也需要和LVS-M一样复制网络端口，设置10.10.10.100的浮动IP，但是与真实服务器不同的是：这个浮动IP没有设置ARP响应级别，所以会出现一个广播地址中有冲突IP的问题，这个服务器不能和真实服务器一样，说要用浮动IP就用，需要等LVS-M宕机后才可以使用浮动IP，KeepAlived实现了ARP的管理功能解决了这个问题。</p>
<ul>
<li><p>按照LVS-DR模式的负载均衡服务器配置机器</p></li>
<li><p>重启网络看到，查看网络状态看到网卡启动失败</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@C7B4 network-scripts<span class="token punctuation">]</span><span class="token comment"># systemctl restart network</span>
<span class="token punctuation">[</span>root@C7B4 network-scripts<span class="token punctuation">]</span><span class="token comment"># systemctl status network</span>
<span class="token punctuation">..</span>.
<span class="token number">4</span>月 <span class="token number">16</span> 09:37:09 C7B4 network<span class="token punctuation">[</span><span class="token number">3026</span><span class="token punctuation">]</span>: ERROR     <span class="token builtin class-name">:</span> <span class="token number">4</span>月 <span class="token number">16</span> 09:37:09 C7B4 network<span class="token punctuation">[</span><span class="token number">3026</span><span class="token punctuation">]</span>: ERROR     <span class="token builtin class-name">:</span> <span class="token punctuation">[</span>/etc/sysconfig/network-scripts/ifup-aliases<span class="token punctuation">]</span> Error, some other <span class="token function">host</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> already uses address <span class="token number">10.10</span>.10.100.
<span class="token punctuation">..</span>.</code></pre></li>
<li><p>修改网卡启动脚本</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">vim</span> /etc/sysconfig/network-scripts/ifup-eth </code></pre>
<p>我们需要搜索报错信息，有空格，要这样搜索：<code>/already\ uses\ address</code></p>
<p>找到如下代码片</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$&#123;REALDEVICE&#125;</span>"</span> <span class="token operator">!=</span> <span class="token string">"lo"</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$&#123;arpcheck<span class="token punctuation">[</span>$idx<span class="token punctuation">]</span>&#125;</span>"</span> <span class="token operator">!=</span> <span class="token string">"no"</span> <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token assign-left variable">ARPING</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span>/sbin/arping -c <span class="token number">2</span> -w $<span class="token punctuation">&#123;</span>ARPING_WAIT:-3<span class="token punctuation">&#125;</span> -D -I $<span class="token punctuation">&#123;</span>REALDEVICE<span class="token punctuation">&#125;</span> $<span class="token punctuation">&#123;</span>ipaddr<span class="token punctuation">[</span>$idx<span class="token punctuation">]</span><span class="token punctuation">&#125;</span><span class="token variable">)</span></span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$?</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token assign-left variable">ARPINGMAC</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $ARPING <span class="token operator">|</span>  <span class="token function">sed</span> -ne <span class="token string">'s/.*\[\(.*\)\].*/\1/p'</span><span class="token variable">)</span></span>
        net_log $<span class="token string">"Error, some other host (<span class="token variable">$ARPINGMAC</span>) already uses address <span class="token variable">$&#123;ipaddr<span class="token punctuation">[</span>$idx<span class="token punctuation">]</span>&#125;</span>."</span>
        <span class="token builtin class-name">exit</span> <span class="token number">1</span>
    <span class="token keyword">fi</span>
<span class="token keyword">fi</span></code></pre>
<p>意思是通过ARP检查是否冲突，冲突就报错终止，解决方法很简单，直接注释掉</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token operator">!</span> <span class="token assign-left variable"><span class="token environment constant">LC_ALL</span></span><span class="token operator">=</span>C <span class="token function">ip</span> addr <span class="token function">ls</span> <span class="token variable">$&#123;REALDEVICE&#125;</span> <span class="token operator">|</span> <span class="token assign-left variable"><span class="token environment constant">LC_ALL</span></span><span class="token operator">=</span>C <span class="token function">grep</span> -q <span class="token string">"<span class="token variable">$&#123;ipaddr<span class="token punctuation">[</span>$idx<span class="token punctuation">]</span>&#125;</span>/<span class="token variable">$&#123;prefix<span class="token punctuation">[</span>$idx<span class="token punctuation">]</span>&#125;</span>"</span> <span class="token punctuation">;</span> <span class="token keyword">then</span>
<span class="token comment">#    if [ "$&#123;REALDEVICE&#125;" != "lo" ] &amp;&amp; [ "$&#123;arpcheck[$idx]&#125;" != "no" ] ; then</span>
<span class="token comment">#    ARPING=$(/sbin/arping -c 2 -w $&#123;ARPING_WAIT:-3&#125; -D -I $&#123;REALDEVICE&#125; $&#123;ipaddr[$idx]&#125;)</span>
<span class="token comment">#        if [ $? = 1 ]; then</span>
<span class="token comment">#            ARPINGMAC=$(echo $ARPING |  sed -ne 's/.*\[\(.*\)\].*/\1/p')</span>
<span class="token comment">#            net_log $"Error, some other host ($ARPINGMAC) already uses address $&#123;ipaddr[$idx]&#125;."</span>
<span class="token comment">#            exit 1</span>
<span class="token comment">#    fi</span>
<span class="token comment">#fi</span>
<span class="token punctuation">..</span>.</code></pre>
<p>重启网络服务开启网卡</p>
<p>复制配置文件，修改state为SLAVE，修改权重为80-50=30</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">scp</span> root@10.10.10.11:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf </code></pre>
<p><strong>实际上，KeepAlived配置文件里面已经写有集群配置文件，可以不配置ipvsadm</strong></p>
<p><strong>测试</strong></p>
<p>ipvs是一个内核服务，不可用的唯一原因是系统崩了，我们关闭主服务器，网页正常负载均衡</p></li>
</ul>
<h4 id="nginxheartbeat实现高可用">Nginx+HeartBeat实现高可用</h4>
<p>HeartBeat是Linux-HA(HighAvailability)的一个组件</p>
<p>需要两台虚拟机：</p>
<ul>
<li>机器C：客户机 物理机 192.168.177.162</li>
<li>机器SR1：Nginx服务器1 虚拟机1 网卡1(enpxxx)：10.10.10.11
网卡1(enpxxx)浮动：10.10.10.100 主机名C7B1</li>
<li>机器SR2：Nginx服务器2 虚拟机2 网卡1(enpxxx)：10.10.10.12
网卡1(enpxxx)浮动：10.10.10.100 主机名C7B2</li>
</ul>
<p>没有负载均衡服务器，两台服务器全跑在公网上，浮动一个10.10.10.100，互相心跳检测，不必担心公网IP上能否浮动IP，修改网卡配置文件后出现冲突IP相当于是进行了一次ARP攻击</p>
<p><strong>双虚拟机配置</strong></p>
<ul>
<li><p>配置yum
163-Base与epel源<code>yum -y install epel-release</code></p></li>
<li><p>双机器 联网 安装Nginx 修改index页面</p></li>
<li><p>在HeartBeat3中，一个项目被拆解成三个子项目了，要去<a
target="_blank" rel="noopener" href="http://www.linux-ha.org/wiki/Download">官网</a>下载三个包：Heartbeat，Cluster
Glue，Resource Agents</p>
<p>安装Cluster Glue</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">yum <span class="token function">install</span> -y libxml2-devel.i686 docbook-style-xsl asciidoc gcc gcc-c++ autoconf automake libnet  libtool glib2-devel libxml2-devel bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel docbook-dtds docboot-style-xsl libtool-ltdl-devel
<span class="token function">tar</span> -xf 0a7add1d9996.tar.bz2 
<span class="token builtin class-name">cd</span> Reusable-Cluster-Components-glue--0a7add1d9996/
./autogen.sh
./configure <span class="token punctuation">\</span>
--prefix<span class="token operator">=</span>/opt/heartbeat/ <span class="token punctuation">\</span>
--sysconfdir<span class="token operator">=</span>/etc/heartbeat <span class="token punctuation">\</span>
<span class="token assign-left variable">libdir</span><span class="token operator">=</span>/opt/heartbeat/lib64 <span class="token punctuation">\</span>
<span class="token assign-left variable">LIBS</span><span class="token operator">=</span><span class="token string">'/usr/lib64/libuuid.so.1'</span>
<span class="token function">make</span>
<span class="token function">make</span> <span class="token function">install</span></code></pre>
<p>安装resource-agents</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">yum -y <span class="token function">install</span> libnet* glib2-devel libtool-ltdl-devel net-snmp-devel bzip2-devel ncurses-devel openssl-devel libtool libxml2-devel gettext gettext-devel bison flex zlib-devel mailx <span class="token function">which</span> libxslt-devel docbook-dtds docbook-style-xsl PyXML shadow-utils opensp e2fsprogs-devel autoconf automake libuuid-devel instltool libaio-devel libxslt-devel gcc <span class="token function">make</span> pkgconfig libxml2 libuuid-devel
<span class="token function">tar</span> -xf resource-agents-3.9.6.tar.gz 
<span class="token builtin class-name">cd</span> resource-agents-3.9.6/
./autogen.sh 
./configure <span class="token punctuation">\</span>
--prefix<span class="token operator">=</span>/opt/heartbeat/ <span class="token punctuation">\</span>
--sysconfdir<span class="token operator">=</span>/etc/heartbeat <span class="token punctuation">\</span>
<span class="token assign-left variable">libdir</span><span class="token operator">=</span>/opt/heartbeat/lib64 <span class="token punctuation">\</span>
<span class="token assign-left variable">CFLAGS</span><span class="token operator">=</span>-I/opt/heartbeat/include <span class="token punctuation">\</span>
<span class="token assign-left variable">LDFLAGS</span><span class="token operator">=</span>-I/opt/heartbeat/lib64 <span class="token punctuation">\</span>
<span class="token assign-left variable">LIBS</span><span class="token operator">=</span><span class="token string">'/usr/lib64/libuuid.so.1'</span>
<span class="token function">ln</span> -s /opt/heartbeat/lib64/* /usr/lib64 
<span class="token function">make</span>
<span class="token function">make</span> <span class="token function">install</span></code></pre>
<p>安装Heartbeat</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token function">tar</span> -xvf 958e11be8686.tar.bz2 
<span class="token builtin class-name">cd</span> Heartbeat-3-0-958e11be8686/
./bootstrap
./configure <span class="token punctuation">\</span>
--prefix<span class="token operator">=</span>/opt/heartbeat <span class="token punctuation">\</span>
--sysconfdir<span class="token operator">=</span>/etc/heartbeat <span class="token punctuation">\</span>
<span class="token assign-left variable">CFLAGS</span><span class="token operator">=</span>-I/opt/heartbeat/include <span class="token punctuation">\</span>
<span class="token assign-left variable">LDFLAGS</span><span class="token operator">=</span>-L/opt/heartbeat/lib64 <span class="token punctuation">\</span>
<span class="token assign-left variable">LIBS</span><span class="token operator">=</span><span class="token string">'/usr/lib64/libuuid.so.1'</span>
<span class="token comment">#修改源码文件，否则会make报错</span>
<span class="token function">vim</span> /opt/heartbeat/include/heartbeat/glue_config.h <span class="token comment"># 注释或者删除最后一行内容</span>
<span class="token function">vim</span> lib/hbclient/Makefile <span class="token comment">#删掉-Werror字段</span>
<span class="token function">make</span>
<span class="token function">make</span> <span class="token function">install</span></code></pre></li>
<li><p>开启时间同步服务：一台服务器安装ntp服务</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">yum <span class="token function">install</span> -y ntp</code></pre>
<p>配置<code>/etc/ntp.conf</code></p>
<pre class="language-bash" data-language="bash"><code class="language-bash">restrict <span class="token number">10.10</span>.10.0 mask <span class="token number">255.255</span>.255.0 nomodify notrap
<span class="token comment"># 上面修改为当前网段 掩码</span>
restrict ::1

<span class="token comment"># Hosts on local network are less restricted.</span>
<span class="token comment">#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap</span>

<span class="token comment"># Use public servers from the pool.ntp.org project.</span>
<span class="token comment"># Please consider joining the pool (http://www.pool.ntp.org/join.html).</span>
<span class="token comment"># server 0.centos.pool.ntp.org iburst	# 注释掉预设的时间服务器</span>
<span class="token comment"># server 1.centos.pool.ntp.org iburst</span>
<span class="token comment"># server 2.centos.pool.ntp.org iburst</span>
<span class="token comment"># server 3.centos.pool.ntp.org iburst</span>
server <span class="token number">127.127</span>.1.0						
<span class="token comment"># 设置时间服务器为本机(本地回环地址:127.0.0.1->127.255.255.254) 必须这样写 </span>
fudge <span class="token number">127.127</span>.1.0 stratum <span class="token number">10</span>
<span class="token comment"># 时间服务器的层次 配置为局域网级别10</span></code></pre>
<p>启动服务<code>systemctl start ntpd</code></p></li>
<li><p>开启时间同步服务：另一台机器安装时间同步客户端</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">yum <span class="token function">install</span> -y ntpdate
ntpdate -u <span class="token number">10.10</span>.10.11		<span class="token comment"># 进行同步</span></code></pre></li>
<li><p>双机器
配置主机名，节点之间是通过主机名heartbeat检测的，不能相同</p></li>
<li><p>双机器
开启主机名解析(正常使用DNS，直接改<code>/etc/hosts</code>也可以)</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token number">10.10</span>.10.11 C7B1
<span class="token number">10.10</span>.10.12 C7B2</code></pre></li>
<li><p>拷贝配置文件</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">cd</span> /opt/heartbeat/share/doc/heartbeat/
<span class="token function">cp</span> ha.cf authkeys haresources /etc/heartbeat/		<span class="token comment"># 配置文件目录默认是/etc/ha.d 我在编译的时候指定了，所以变了</span></code></pre></li>
<li><p>配置认证文件<code>authkeys</code></p>
<p><code>dd if=/dev/random bs=512 count=1 | openssl md5</code>配置文件需要一个md5码作为密钥，我们可以使用dd随机生成一个，将文件修改为</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">auth <span class="token number">3</span>				<span class="token comment"># 选择第三个认证方式</span>
<span class="token number">1</span> crc
<span class="token number">2</span> sha1 HI<span class="token operator">!</span>
<span class="token number">3</span> md5  3a987d0f396bb9e29606148a8c1eb99f		<span class="token comment"># 使用md5</span></code></pre>
<p>修改配置文件权限600，否则和vsftp一样会报错安全性</p></li>
<li><p>配置主配置文件<code>ha.cf</code></p>
<pre class="language-diff" data-language="diff"><code class="language-diff"><span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"> #bcast  eth0             # Linux
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line"> bcast  enp0s3            # 使用哪张网卡做心跳检测
</span></span><span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"> #node   ken3
</span><span class="token prefix deleted">-</span><span class="token line"> #node   kathy
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line"> node    C7B1
</span><span class="token prefix inserted">+</span><span class="token line"> node    C7B2				# 所有主机名</span></span></code></pre></li>
<li><p>配置脚本文件<code>haresources</code>：Heartbeat默认提供了很多的配置文件，我们选的这个是用来做IP切换的</p>
<pre class="language-diff" data-language="diff"><code class="language-diff"><span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line"> #node1  10.0.0.170 Filesystem::/dev/sda1::/data1::ext2
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line"> C7B1 IPaddr::10.10.10.100/24/enp0s3:0		# 注意修改主机名</span></span></code></pre>
<p>这个脚本的作用相当于为我们配置了网卡接口，同时还做好了防止冲突，正确切换的操作</p></li>
<li><p>测试：正常可以访问，关掉一个机器可以访问另一个，注意，这个与之前修改网卡配置不同，这个是哪个服务器工作，哪个服务器才复制端口，非主机是不复制网卡的</p></li>
</ul>
<p><strong>注意，heartBeat是检测网络是否可用判断是否宕机，如果网络正常Nginx宕机，是不会切换的</strong></p>
<p>修复思路是：循环检测，如果发现Nginx宕机，立刻关闭heartBeat</p>
<pre class="language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span>$ <span class="token function">mkdir</span> /usr/local/script
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span>$ <span class="token function">vim</span> <span class="token number">80</span>.sh
<span class="token comment">#!/bin/bash</span>

<span class="token assign-left variable"><span class="token environment constant">PWD</span></span><span class="token operator">=</span>/usr/local/script/

<span class="token assign-left variable">URL</span><span class="token operator">=</span><span class="token string">"http://10.10.10.11/index.html"</span>

<span class="token assign-left variable">HTTP_CODE</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">curl</span> -o /dev/null -s -w <span class="token string">"%&#123;http_code&#125;"</span> <span class="token string">"<span class="token variable">$&#123;URL&#125;</span>"</span><span class="token variable">`</span></span> 


<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$HTTP_CODE</span> <span class="token operator">!=</span> <span class="token number">200</span> <span class="token punctuation">]</span>
    <span class="token keyword">then</span>
	<span class="token function">service</span> heartbeat stop
<span class="token keyword">fi</span>
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span>$ <span class="token function">crontab</span> -e
*/1 * * * * <span class="token function">bash</span> /usr/local/script/80.sh</code></pre>
<h3 id="多级负载">多级负载</h3>
<p>X层负载：服务运行在OSI七层的哪一层，例如七层负载的Nginx，四层负载的LVS</p>
<p>Nginx负载均衡支持虚拟主机，LVS支持高负载，考虑只有一个公网IP，想要做虚拟主机到两个网站，但是需要LVS高负载，可以使用多级负载，原理是：客户访问到LVS服务器，LVS负责了多个Nginx的负载均衡(一个Nginx的话LVS就没有意义了)，Nginx做虚拟主机负载均衡到Apache</p>
<p>需要六台虚拟机：</p>
<ul>
<li>机器C：客户机 物理机 192.168.177.162</li>
<li>机器LVS-DR：LVS负载均衡服务器 虚拟机1 网卡1(enpxxx)：10.10.10.11
网卡1(enpxxx)浮动：10.10.10.100</li>
<li>机器Ngx1：Nginx虚拟主机负载均衡服务器1 虚拟机2
网卡1(enpxxx)：10.10.10.12 网卡1(enpxxx)浮动：10.10.10.100</li>
<li>机器Ngx2：Nginx虚拟主机负载均衡服务器2 虚拟机3
网卡1(enpxxx)：10.10.10.13 网卡1(enpxxx)浮动：10.10.10.100</li>
<li>机器RS1：Apache服务器1-1 虚拟机1 网卡1(enpxxx)：10.10.10.14</li>
<li>机器RS2：Apache服务器1-2 虚拟机2 网卡1(enpxxx)：10.10.10.15</li>
<li>机器RS3：Apache服务器2-1 虚拟机3 网卡1(enpxxx)：10.10.10.16</li>
</ul>
<p><strong>机器RS1,2,3</strong></p>
<ul>
<li>开启Apache，分别写入页面</li>
</ul>
<p><strong>机器Ngx1,2</strong></p>
<ul>
<li><p>安装Nginx</p></li>
<li><p>配置文件</p>
<pre class="language-bash" data-language="bash"><code class="language-bash">upstream demo1.com<span class="token punctuation">&#123;</span>
    server <span class="token number">10.10</span>.10.14:80<span class="token punctuation">;</span>
    server <span class="token number">10.10</span>.10.15:80<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
  
upstream demo2.com<span class="token punctuation">&#123;</span>
    server <span class="token number">10.10</span>.10.16:80<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
  
server <span class="token punctuation">&#123;</span>
    listen       <span class="token number">80</span><span class="token punctuation">;</span>
    server_name  www.demo1.com<span class="token punctuation">;</span>
    location / <span class="token punctuation">&#123;</span>
        proxy_pass http://demo1.com<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
  
server <span class="token punctuation">&#123;</span>
    listen       <span class="token number">80</span><span class="token punctuation">;</span>
    server_name  www.demo2.com<span class="token punctuation">;</span>
    location / <span class="token punctuation">&#123;</span>
        proxy_pass http://demo2.com<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span></code></pre></li>
<li><p>物理机修改Host文件测试</p></li>
</ul>
<p><strong>LVS-DR配置</strong></p>
<p>按照LVS-DR配置LVS服务器，Nginx作为“真实服务器”</p>
<p><strong>测试</strong></p>
<p>物理机修改host测试</p>

    </div>

    
    
    
      


    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.png" alt="Liu Kairui 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.png" alt="Liu Kairui 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Liu Kairui
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://liukairui.me/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" title="Linux集群教程笔记">http://liukairui.me/article/Linux集群教程笔记/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/%E8%BF%90%E7%BB%B4/" rel="tag"><i class="fa fa-tag"></i> 运维</a>
              <a href="/tags/Linux/" rel="tag"><i class="fa fa-tag"></i> Linux</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/article/Vue23%E5%9F%BA%E7%A1%80/" rel="prev" title="Vue2/3入门笔记">
                  <i class="fa fa-chevron-left"></i> Vue2/3入门笔记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/article/2022%E5%AF%92%E5%81%87%E7%99%BE%E5%BA%A6%E5%89%8D%E7%AB%AF%E8%AE%AD%E7%BB%83%E8%90%A5%E7%AC%94%E8%AE%B0/" rel="next" title="2022寒假百度前端训练营笔记">
                  2022寒假百度前端训练营笔记 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="valine-comments"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2019 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Liu Kairui</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>
  <div class="addthis_inline_share_toolbox">
    <script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-6231eb6f709fc868" async="async"></script>
  </div><script
  async
  src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"
></script>

 
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment-precise-range-plugin@1.3.0/moment-precise-range.min.js"></script>
<script>
  function timer() {
    var ages = moment.preciseDiff(moment(),moment(20201101,"YYYYMMDD"));
    ages = ages.replace(/years?/, "年");
    ages = ages.replace(/months?/, "月");
    ages = ages.replace(/days?/, "天");
    ages = ages.replace(/hours?/, "小时");
    ages = ages.replace(/minutes?/, "分");
    ages = ages.replace(/seconds?/, "秒");
    ages = ages.replace(/\d+/g, '<span class="daysCnt" style="color:#1890ff">$&</span>');
    div.innerHTML = `我已在此等候你 ${ages}`;
    div.className="workDays";
  }
  var div = document.createElement("div");
  //插入到copyright之后
  var copyright = document.querySelector(".copyright");
  document.querySelector(".footer-inner").insertBefore(div, copyright.nextSibling);
  timer();
  setInterval("timer()",1000)
</script>



    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@next-theme/pjax@0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>


  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":"forest","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.14.0/mermaid.min.js","integrity":"sha256-7wT34TI0pEBeEFoi4z+vhuSddGh6vUTMWdqJ2SDe2jg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>

  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


  <script class="next-config" data-name="leancloud_visitors" type="application/json">{"enable":true,"app_id":"ABKlVtS4cyaWYEwunPyK3sXt-9Nh9j0Va","app_key":"xxGXdTTEGEVifs2TLB35844I","server_url":"https://abklvts4.lc-cn-e1-shared.com","security":false}</script>
  <script src="/js/third-party/statistics/lean-analytics.js"></script>


  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","mhchem":true,"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.min.js"}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.2.0/quicklink.umd.js" integrity="sha256-4kQf9z5ntdQrzsBC3YSHnEz02Z9C1UeW/E9OgnvlzSY=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":false,"archive":false,"delay":true,"timeout":3000,"priority":true,"url":"http://liukairui.me/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>

<script>
var options = {
  bottom: '64px',
  right: 'unset',
  left: '32px',
  time: '0.5s',
  mixColor: 'transparent',
  backgroundColor: 'transparent',
  buttonColorDark: '#100f2c',
  buttonColorLight: '#fff',
  saveInCookies: true,
  label: '🌓',
  autoMatchOsTheme: true
}
const darkmode = new Darkmode(options);
window.darkmode = darkmode;
darkmode.showWidget();
</script>


<script class="next-config" data-name="valine" type="application/json">{"enable":true,"appId":"ABKlVtS4cyaWYEwunPyK3sXt-9Nh9j0Va","appKey":"xxGXdTTEGEVifs2TLB35844I","serverURLs":"https://abklvts4.lc-cn-e1-shared.com","placeholder":"请开始你的表演","avatar":"identicon","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-CN","visitor":false,"comment_count":true,"recordIP":true,"enableQQ":true,"requiredFields":[],"el":"#valine-comments","path":"/article/Linux%E9%9B%86%E7%BE%A4%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/"}</script>
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.valine.el)
    .then(() => NexT.utils.getScript(
      'https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js',
      { condition: window.Valine }
    ))
    .then(() => {
      new Valine(CONFIG.valine);
    });
});
</script>


  <script async src="/js/fireworks.js"></script>




  <script src="/js/activate-power-mode.min.js"></script>
  <script>
    POWERMODE.colorful = true;
    POWERMODE.shake = false;
    document.body.addEventListener('input', POWERMODE);
  </script>




  <script src="/js/wobblewindow.js"></script>
  <script>
    //只在桌面版网页启用特效 记得同步到header.njk
    if( window.innerWidth > 768  ){
      $(document).ready(function () {
        
          $('body>main>header').wobbleWindow({
            radius: 50,
            movementTop: false,
            movementLeft: false,
            movementRight: false,
            debug: false,
          });
        

        //
        //  $('body header>aside').wobbleWindow({
        //    radius: 50,
        //    movementLeft: false,
        //    movementTop: false,
        //    movementBottom: false,
        //    position: 'fixed',
        //    debug: false,
        //  });
        //

        
          $('body>footer').wobbleWindow({
            radius: 50,
            movementBottom: false,
            movementLeft: false,
            movementRight: false,
            debug: false,
          });
        
      });
    }
  </script>


 
<script>
  $(document).ready(function(){
    var beian = document.querySelector(".footer-inner > .beian > a");
    var cpr=document.querySelector(".footer-inner > .copyright");
    var cnz=document.querySelector(".workDays");//.parentNode;
    var bsz=document.querySelector(".busuanzi-count");
    if(cnz!=null&&bsz!=null)
      cnz.parentNode.insertBefore(bsz,cnz);
    if(bsz == null || bsz == undefined || document.querySelector("#busuanzi_value_site_pv").innerText=="" || document.querySelector("#busuanzi_value_site_uv").innerText == "")
      bsz.remove();
    checkIndex();
    if(beian){cpr.classList.add('split-line');cpr.appendChild(beian);}
  })
  $(document).on('pjax:complete',checkIndex);
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacity":0.7}});</script></body>
</html>
